﻿Imports Crystal.CLL
Imports Infragistics.Win.UltraWinGrid
Imports CrystalDataHub.BLL
Imports Crystal.CustomControls
Imports Infragistics.Win

Public Class CreateInvestorFund
    Inherits BaseForm

#Region "Fields"
    Private mclsCreateInvestorFundManager As New CreateInvestorFundManager
    Private mdtCreateNew As New DataTable
    Private mstrCreateNew As String = "Create New"
    Private mstrMappedInvName As String = "Mapped Inv. Name"
    Private mstrMappedFundName As String = "Mapped Fund Name"
    Public mstrUnmapped As String = "UNMAPPED"
    Private mdtCounty As New DataTable
    Private marrAllowEditInvestor() As String = {mstrMappedInvName, "Investor Type", "Master Investor Type", "Head Investor", "Style", "Turnover", "City", "State", "Country Name"}
    Private marrAllowEditFund() As String = {mstrMappedFundName, "Fund Type", "Style", "Turnover", "Country Name"}
    Public mstrMatchCol3 As String
    Public mintSourceTypeID As Int16
    Public mdtNewInvestors As New DataTable
    Public mdtNewFunds As New DataTable
    Public mdtColMapping As New DataTable
    Public menmSourceTypeCode As SourceTypeCode

    Private mintStyleIndex As Integer
    Private mintTurnoverIndex As Integer
    Private mintCityIndex As Integer
    Private mintStateIndex As Integer
    Private mintCountryIndex As Integer

    Private mintPublicStyleIndex As Integer
    Private mintPublicTurnoverIndex As Integer
    Private mintPublicCityIndex As Integer
    Private mintPublicStateIndex As Integer
    Private mintPublicCountryIndex As Integer

    Private mintInvestorClickedRowIndex As Integer
    Private mintFundClickedRowIndex As Integer

    Private mblnIsPublicLoaded As Boolean = False
    Private mstrScreenName As String = "Create Investor Fund"

    Public mintProjectID As Integer
#End Region

#Region "Constructors"
    Sub New(ByVal ldtCreateNew As DataTable)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mdtCreateNew = ldtCreateNew
    End Sub
#End Region

#Region "On Load"

    Private Sub CreateInvestorFund_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CallOnLoad()
    End Sub

    Private Sub CallOnLoad()
        SetOnLoadControls()
        Application.DoEvents()
        InitLoadAllCombos()
        LoadInvestorData()
        LoadFundData()
        mblnIsPublicLoaded = True
    End Sub

    Public Sub SetOnLoadControls()
        Me.Text = TitleManager.GetTitle("CreateInvestorFund")
        Me.WindowState = FormWindowState.Maximized
        Me.grdInvestors.DisplayLayout.Override.AllowUpdate = Infragistics.Win.DefaultableBoolean.[True]
        Me.grdFunds.DisplayLayout.Override.AllowUpdate = Infragistics.Win.DefaultableBoolean.[True]

        grdFinalizeInvestor.Size = New Drawing.Size(grdFinalizeInvestor.Width, Me.Height / 2 - 40)
        grdFinalizeFund.Location = New Point(grdFinalizeFund.Location.X, grdFinalizeInvestor.Height + 10)
        grdFinalizeFund.Size = New Drawing.Size(grdFinalizeFund.Width, Me.Height / 2 - 40)
    End Sub

    ''' <summary>
    ''' To load the investors to be created
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadInvestorData()
        Dim lintAddAtrributesInvestorStartIndex As Integer
        Dim ldtCreateNewInvestor As New DataTable
        Dim dv As DataView
        Try
            ldtCreateNewInvestor = mdtCreateNew.Copy
            lintAddAtrributesInvestorStartIndex = ldtCreateNewInvestor.Columns(mstrMappedFundName).Ordinal + 1

            'Add/Remove the attributes
            ldtCreateNewInvestor.Columns.Remove("FundID")
            ldtCreateNewInvestor.Columns.Remove("FundEntityID")

            ldtCreateNewInvestor.Columns.Add("Investor Type", GetType(String))
            ldtCreateNewInvestor.Columns.Add("Investor Type Name", GetType(String))
            ldtCreateNewInvestor.Columns("Investor Type").SetOrdinal(lintAddAtrributesInvestorStartIndex)

            ldtCreateNewInvestor.Columns.Add("Master Investor Type", GetType(String))
            ldtCreateNewInvestor.Columns.Add("Master Investor Type Name", GetType(String))
            ldtCreateNewInvestor.Columns("Master Investor Type").SetOrdinal(lintAddAtrributesInvestorStartIndex + 1)

            ldtCreateNewInvestor.Columns.Add("Head Investor", GetType(String))
            ldtCreateNewInvestor.Columns.Add("Head Investor Name", GetType(String))
            ldtCreateNewInvestor.Columns("Head Investor").SetOrdinal(lintAddAtrributesInvestorStartIndex + 2)

            ldtCreateNewInvestor.Columns.Add("Style", GetType(String))
            ldtCreateNewInvestor.Columns.Add("Style Name", GetType(String))
            ldtCreateNewInvestor.Columns("Style").SetOrdinal(lintAddAtrributesInvestorStartIndex + 3)

            ldtCreateNewInvestor.Columns.Add("Turnover", GetType(String))
            ldtCreateNewInvestor.Columns.Add("Turnover Name", GetType(String))
            ldtCreateNewInvestor.Columns("Turnover").SetOrdinal(lintAddAtrributesInvestorStartIndex + 4)

            ldtCreateNewInvestor.Columns.Add("City", GetType(String))
            ldtCreateNewInvestor.Columns.Add("City Name", GetType(String))
            ldtCreateNewInvestor.Columns("City").SetOrdinal(lintAddAtrributesInvestorStartIndex + 5)

            ldtCreateNewInvestor.Columns.Add("State", GetType(String))
            ldtCreateNewInvestor.Columns.Add("State Name", GetType(String))
            ldtCreateNewInvestor.Columns("State").SetOrdinal(lintAddAtrributesInvestorStartIndex + 6)

            ldtCreateNewInvestor.Columns.Add("Country Name", GetType(String))
            ldtCreateNewInvestor.Columns.Add("CountryName", GetType(String))
            ldtCreateNewInvestor.Columns("Country Name").SetOrdinal(lintAddAtrributesInvestorStartIndex + 7)

            lintAddAtrributesInvestorStartIndex = ldtCreateNewInvestor.Columns("Country Name").Ordinal + 1
            ldtCreateNewInvestor.Columns("Investor Type Name").SetOrdinal(lintAddAtrributesInvestorStartIndex)
            ldtCreateNewInvestor.Columns("Master Investor Type Name").SetOrdinal(lintAddAtrributesInvestorStartIndex + 1)
            ldtCreateNewInvestor.Columns("Head Investor Name").SetOrdinal(lintAddAtrributesInvestorStartIndex + 2)
            ldtCreateNewInvestor.Columns("Style Name").SetOrdinal(lintAddAtrributesInvestorStartIndex + 3)
            ldtCreateNewInvestor.Columns("Turnover Name").SetOrdinal(lintAddAtrributesInvestorStartIndex + 4)
            ldtCreateNewInvestor.Columns("City Name").SetOrdinal(lintAddAtrributesInvestorStartIndex + 5)
            ldtCreateNewInvestor.Columns("State Name").SetOrdinal(lintAddAtrributesInvestorStartIndex + 6)
            ldtCreateNewInvestor.Columns("CountryName").SetOrdinal(lintAddAtrributesInvestorStartIndex + 7)

            'Filter the CreateNew Investor rows.
            dv = New DataView(ldtCreateNewInvestor)
            dv.RowFilter = "[" + mstrMappedInvName + "]='" + mstrCreateNew + "'"
            grdInvestors.DataSource = dv.ToTable

            'Set the Investor Name text to blank
            For Each lRow As UltraGridRow In grdInvestors.Rows
                lRow.Cells(mstrMappedInvName).Value = ""
            Next
            grdInvestors.UpdateData()

            grdFinalizeInvestor.DataSource = CType(grdInvestors.DataSource, DataTable).Copy

            'If the SourceTypeCode is Public, then set the attributes to be inherited from Excel data.
            If menmSourceTypeCode = SourceTypeCode.PBC Then
                SetPublicAttributes(ldtCreateNewInvestor, grdInvestors, True)
            End If
            grdInvestors.Rows.Band.Columns(mstrMappedInvName).Width = 250
            grdInvestors.Rows.Band.Columns(mstrMappedFundName).Width = 250
            grdFinalizeInvestor.Rows.Band.Columns(mstrMappedInvName).Width = 250
            grdFinalizeInvestor.Rows.Band.Columns(mstrMappedFundName).Width = 250
            SetInvestorColumnsVisibility(grdInvestors)
            SetFinalizeInvestorColumnVisibility(grdFinalizeInvestor)
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        Finally
            ldtCreateNewInvestor = Nothing
            dv = Nothing
        End Try
    End Sub

    Private Sub SetInvestorColumnsVisibility(ByVal grdData As CrystalUltraGrid)
        grdData.DisplayLayout.Bands(0).Columns("InvestorID").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("InvEntityID").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("RowNumber").Hidden = True

        grdData.DisplayLayout.Bands(0).Columns("Investor Type Name").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Master Investor Type Name").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Head Investor Name").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Style Name").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Turnover Name").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("City Name").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("State Name").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("CountryName").Hidden = True

    End Sub

    Private Sub SetFinalizeInvestorColumnVisibility(ByVal grdData As CrystalUltraGrid)
        grdData.DisplayLayout.Bands(0).Columns("InvestorID").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("InvEntityID").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("RowNumber").Hidden = True

        grdData.DisplayLayout.Bands(0).Columns("Investor Type").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Master Investor Type").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Head Investor").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Style").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Turnover").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("City").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("State").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Country Name").Hidden = True
    End Sub

    Private Sub LoadFundData()
        Dim lintAddAtrributesFundStartIndex As Integer
        Dim ldtCreateNewFund As New DataTable
        Dim dv As DataView
        Try
            ldtCreateNewFund = mdtCreateNew.Copy
            lintAddAtrributesFundStartIndex = ldtCreateNewFund.Columns(mstrMappedFundName).Ordinal + 1

            'Add/Remove the attributes
            ldtCreateNewFund.Columns.Remove("InvestorID")
            ldtCreateNewFund.Columns.Remove("InvEntityID")

            ldtCreateNewFund.Columns.Add("Fund Type", GetType(String))
            ldtCreateNewFund.Columns.Add("Fund Type Name", GetType(String))
            ldtCreateNewFund.Columns("Fund Type").SetOrdinal(lintAddAtrributesFundStartIndex)

            ldtCreateNewFund.Columns.Add("Style", GetType(String))
            ldtCreateNewFund.Columns.Add("Style Name", GetType(String))
            ldtCreateNewFund.Columns("Style").SetOrdinal(lintAddAtrributesFundStartIndex + 1)

            ldtCreateNewFund.Columns.Add("Turnover", GetType(String))
            ldtCreateNewFund.Columns.Add("Turnover Name", GetType(String))
            ldtCreateNewFund.Columns("Turnover").SetOrdinal(lintAddAtrributesFundStartIndex + 2)

            ldtCreateNewFund.Columns.Add("Country Name", GetType(String))
            ldtCreateNewFund.Columns.Add("CountryName", GetType(String))
            ldtCreateNewFund.Columns("Country Name").SetOrdinal(lintAddAtrributesFundStartIndex + 3)

            lintAddAtrributesFundStartIndex = ldtCreateNewFund.Columns("Country Name").Ordinal + 1
            ldtCreateNewFund.Columns("Fund Type Name").SetOrdinal(lintAddAtrributesFundStartIndex)
            ldtCreateNewFund.Columns("Style Name").SetOrdinal(lintAddAtrributesFundStartIndex + 1)
            ldtCreateNewFund.Columns("Turnover Name").SetOrdinal(lintAddAtrributesFundStartIndex + 2)
            ldtCreateNewFund.Columns("CountryName").SetOrdinal(lintAddAtrributesFundStartIndex + 3)

            'Filter the CreateNew Fund rows.
            dv = New DataView(ldtCreateNewFund)
            dv.RowFilter = "[" + mstrMappedFundName + "]='" + mstrCreateNew + "'"
            grdFunds.DataSource = dv.ToTable

            grdFinalizeFund.DataSource = CType(grdFunds.DataSource, DataTable).Copy

            'If the SourceTypeCode is Public, then set the attributes to be inherited from Excel data.
            If menmSourceTypeCode = SourceTypeCode.PBC Then
                SetPublicAttributes(ldtCreateNewFund, grdFunds, False)
            End If

            'Set the Fund Name text to blank
            For Each lRow As UltraGridRow In grdFunds.Rows
                If lRow.GetCellValue(lRow.Cells(mstrMappedFundName).Column).Equals(mstrCreateNew) Then
                    lRow.Cells(mstrMappedFundName).Value = ""
                End If
            Next
            grdFunds.UpdateData()

            grdFunds.Rows.Band.Columns(mstrMappedInvName).Width = 250
            grdFunds.Rows.Band.Columns(mstrMappedFundName).Width = 250
            grdFinalizeFund.Rows.Band.Columns(mstrMappedInvName).Width = 250
            grdFinalizeFund.Rows.Band.Columns(mstrMappedFundName).Width = 250

            SetFundColumnsVisibility(grdFunds)
            SetFinalizeFundColumnsVisibility(grdFinalizeFund)
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        Finally
            ldtCreateNewFund = Nothing
            dv = Nothing
        End Try
    End Sub

    Private Sub SetFundColumnsVisibility(ByVal grdData As CrystalUltraGrid)
        grdData.DisplayLayout.Bands(0).Columns("FundID").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("RowNumber").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("FundEntityID").Hidden = True

        grdData.DisplayLayout.Bands(0).Columns("Fund Type Name").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Style Name").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Turnover Name").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("CountryName").Hidden = True
    End Sub

    Private Sub SetFinalizeFundColumnsVisibility(ByVal grdData As CrystalUltraGrid)
        grdData.DisplayLayout.Bands(0).Columns("FundID").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("RowNumber").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("FundEntityID").Hidden = True

        grdData.DisplayLayout.Bands(0).Columns("Fund Type").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Style").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Turnover").Hidden = True
        grdData.DisplayLayout.Bands(0).Columns("Country Name").Hidden = True
    End Sub

    ''' <summary>
    ''' Set the attributes for Public EDS
    ''' </summary>
    ''' <param name="ldtCreateNew"></param>
    ''' <param name="grdData"></param>
    ''' <param name="pblnFromInvestor"></param>
    ''' <remarks></remarks>
    Private Sub SetPublicAttributes(ByVal ldtCreateNew As DataTable, ByVal grdData As UltraGrid, ByVal pblnFromInvestor As Boolean)
        Try
            If menmSourceTypeCode = SourceTypeCode.PBC Then
                mintStyleIndex = ldtCreateNew.Columns("Style").Ordinal
                mintTurnoverIndex = ldtCreateNew.Columns("Turnover").Ordinal
                'Get City and State Index in case of Investors
                If pblnFromInvestor Then
                    mintCityIndex = ldtCreateNew.Columns("City").Ordinal
                    mintStateIndex = ldtCreateNew.Columns("State").Ordinal
                End If
                mintCountryIndex = ldtCreateNew.Columns("Country Name").Ordinal

                For Each lRow As DataRow In mdtColMapping.Rows
                    If lRow.Item("DBFieldName") = "Style" Then
                        mintPublicStyleIndex = ldtCreateNew.Columns(lRow.Item("Map Field").ToString).Ordinal
                    ElseIf lRow.Item("DBFieldName") = "Turnover" Then
                        mintPublicTurnoverIndex = ldtCreateNew.Columns(lRow.Item("Map Field").ToString).Ordinal
                    ElseIf lRow.Item("DBFieldName") = "City" AndAlso pblnFromInvestor Then
                        mintPublicCityIndex = ldtCreateNew.Columns(lRow.Item("Map Field").ToString).Ordinal
                    ElseIf lRow.Item("DBFieldName") = "State" AndAlso pblnFromInvestor Then
                        mintPublicStateIndex = ldtCreateNew.Columns(lRow.Item("Map Field").ToString).Ordinal
                    ElseIf lRow.Item("DBFieldName") = "Country" AndAlso pblnFromInvestor Then
                        mintPublicCountryIndex = ldtCreateNew.Columns(lRow.Item("Map Field").ToString).Ordinal
                    End If
                Next

                'Set the attributes' ID values from the attributes' values
                Dim larrAttribute() As DataRow

                For Each lRow As UltraGridRow In grdData.Rows
                    'lstrQuery = "Style ='" + lRow.Cells(mintPublicStyleIndex).Value + "'"
                    larrAttribute = CType(cmbStyle.DataSource, DataTable).Select("Style ='" + lRow.GetCellValue(lRow.Cells(mintPublicStyleIndex).Column).ToString.Replace("N/A", "NA") + "'")
                    If larrAttribute.Length = 1 Then
                        lRow.Cells(mintStyleIndex).Value = larrAttribute(0).Item("StyleID")
                    End If

                    larrAttribute = CType(cmbTurnover.DataSource, DataTable).Select("Turnover ='" + lRow.GetCellValue(lRow.Cells(mintPublicTurnoverIndex).Column).ToString.Replace("N/A", "NA") + "'")
                    If larrAttribute.Length = 1 Then
                        lRow.Cells(mintTurnoverIndex).Value = larrAttribute(0).Item("TurnoverID")
                    End If

                    If pblnFromInvestor Then
                        larrAttribute = CType(cmbCity.DataSource, DataTable).Select("City ='" + lRow.GetCellValue(lRow.Cells(mintPublicCityIndex).Column).ToString.Replace("'", "''") + "'")
                        If larrAttribute.Length = 1 Then
                            lRow.Cells(mintCityIndex).Value = larrAttribute(0).Item("CityID")
                        End If

                        larrAttribute = CType(cmbState.DataSource, DataTable).Select("State ='" + lRow.GetCellValue(lRow.Cells(mintPublicStateIndex).Column).ToString.Replace("'", "''") + "'")
                        If larrAttribute.Length = 1 Then
                            lRow.Cells(mintStateIndex).Value = larrAttribute(0).Item("StateID")
                        End If
                    End If

                    larrAttribute = CType(cmbCountry.DataSource, DataTable).Select("Country ='" + lRow.GetCellValue(lRow.Cells(mintPublicCountryIndex).Column).ToString.Replace("'", "''") + "'")
                    If larrAttribute.Length = 1 Then
                        lRow.Cells(mintCountryIndex).Value = larrAttribute(0).Item("CountryID")
                    End If
                Next
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        End Try
    End Sub

#End Region

#Region "Events"

#Region "Form Events"

#End Region

#Region "Grid Events"

    Private Sub grdFunds_AfterCellUpdate(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.CellEventArgs) Handles grdFunds.AfterCellUpdate
        Try
            If grdFunds.ActiveRow IsNot Nothing Then
                grdFunds.ActiveRow.Update()
            End If
            If Not e.Cell.Column.Header.Caption.Equals(mstrMappedFundName) Then
                Dim lstrIDCol As String = ""
                lstrIDCol = GetCorrespondingIDColumn(e.Cell.Column.Header.Caption)
                If grdFunds.Rows.Band.Columns.Exists(lstrIDCol) Then
                    If mblnIsPublicLoaded = False AndAlso menmSourceTypeCode = SourceTypeCode.PBC Then
                        grdFunds.Rows(e.Cell.Row.Index).Cells(lstrIDCol).Value = e.Cell.Text
                    Else
                        If CType(e.Cell.Column.EditorComponent, UltraCombo).Value Is Nothing Then
                            grdFunds.Rows(e.Cell.Row.Index).Cells(lstrIDCol).Value = ""
                        Else
                            grdFunds.Rows(e.Cell.Row.Index).Cells(lstrIDCol).Value = CType(e.Cell.Column.EditorComponent, UltraCombo).Text
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        End Try
    End Sub

    Private Sub grdInvestors_AfterCellUpdate(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.CellEventArgs) Handles grdInvestors.AfterCellUpdate
        Try
            If grdInvestors.ActiveRow IsNot Nothing Then
                grdInvestors.ActiveRow.Update()
            End If
            ''Updating the text property in the hidden columns that will be used in the third tab in Finalize tab to show to the user
            If Not e.Cell.Column.Header.Caption.Equals(mstrMappedInvName) Then
                Dim lstrIDCol As String = ""
                lstrIDCol = GetCorrespondingIDColumn(e.Cell.Column.Header.Caption)
                If grdInvestors.Rows.Band.Columns.Exists(lstrIDCol) Then
                    If mblnIsPublicLoaded = False AndAlso menmSourceTypeCode = SourceTypeCode.PBC Then
                        grdInvestors.Rows(e.Cell.Row.Index).Cells(lstrIDCol).Value = e.Cell.Text
                    Else
                        If CType(e.Cell.Column.EditorComponent, UltraCombo).Value Is Nothing Then
                            grdInvestors.Rows(e.Cell.Row.Index).Cells(lstrIDCol).Value = ""
                        Else
                            grdInvestors.Rows(e.Cell.Row.Index).Cells(lstrIDCol).Value = CType(e.Cell.Column.EditorComponent, UltraCombo).Text
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        End Try
    End Sub

    Private Function GetCorrespondingIDColumn(ByVal pstrNameCol As String) As String
        Dim lstrIDCol As String = ""
        Select Case pstrNameCol
            Case "Investor Type"
                lstrIDCol = "Investor Type Name"
            Case "Fund Type"
                lstrIDCol = "Fund Type Name"
            Case "Master Investor Type"
                lstrIDCol = "Master Investor Type Name"
            Case "Head Investor"
                lstrIDCol = "Head Investor Name"
            Case "Style"
                lstrIDCol = "Style Name"
            Case "Turnover"
                lstrIDCol = "Turnover Name"
            Case "City"
                lstrIDCol = "City Name"
            Case "State"
                lstrIDCol = "State Name"
            Case "Country Name"
                lstrIDCol = "CountryName"
        End Select
        Return lstrIDCol
    End Function

    Private Function HasChildRepeatedRowAccessForEnter(ByVal pstrInvestorName As String, ByVal lintCurrentRowIndex As Integer) As Boolean
        Dim lblnStatus As Boolean = False
        Dim ldtInvestorData As DataTable = Nothing
        Dim larrRepeatedInvestorName() As DataRow = Nothing
        Dim lstrQuery As String
        Dim lRow As DataRow = Nothing
        Try
            If pstrInvestorName.Equals("") Then Return False
            grdInvestors.UpdateData()
            ldtInvestorData = grdInvestors.DataSource
            ldtInvestorData.AcceptChanges()
            lstrQuery = "[" + mstrMappedInvName + "] ='" + pstrInvestorName + "'"
            larrRepeatedInvestorName = ldtInvestorData.Select(lstrQuery)
            If larrRepeatedInvestorName.Length > 1 Then
                lRow = ldtInvestorData.Rows(lintCurrentRowIndex)
                If (lRow.Item("Investor Type").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) AndAlso lRow.Item("Master Investor Type").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) _
                    AndAlso lRow.Item("Head Investor").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) AndAlso lRow.Item("Style").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) _
                    AndAlso lRow.Item("Turnover").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) AndAlso lRow.Item("City").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) _
                            AndAlso lRow.Item("State").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) AndAlso lRow.Item("Country Name").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString)) _
                        Then
                    Return True
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
            Return False
        Finally
            ldtInvestorData = Nothing
            larrRepeatedInvestorName = Nothing
        End Try
        Return False
    End Function

    Private Sub SetInvestorRowAsRepeated(ByRef pugrRow As UltraGridRow)
        pugrRow.Cells("Investor Type").Value = -1
        pugrRow.Cells("Master Investor Type").Value = -1
        pugrRow.Cells("Head Investor").Value = -1
        pugrRow.Cells("Style").Value = -1
        pugrRow.Cells("Turnover").Value = -1
        pugrRow.Cells("City").Value = -1
        pugrRow.Cells("State").Value = -1
        pugrRow.Cells("Country Name").Value = -1
    End Sub

    Private Sub SetFundRowAsRepeated(ByRef pugrRow As UltraGridRow)
        pugrRow.Cells("Fund Type").Value = -1
        pugrRow.Cells("Style").Value = -1
        pugrRow.Cells("Turnover").Value = -1
        pugrRow.Cells("Country Name").Value = -1
    End Sub

    Private Function HasMainRepeatedRowForExit(ByVal pstrInvestorName As String, ByVal pintCurrentRowIndex As Integer) As Boolean
        Dim lblnStatus As Boolean = False
        Dim ldtInvestorData As DataTable = Nothing
        Dim larrRepeatedInvestorName() As DataRow = Nothing
        Dim lstrQuery As String
        Try
            If pstrInvestorName.Equals("") Then Return False
            grdInvestors.UpdateData()
            ldtInvestorData = grdInvestors.DataSource
            ldtInvestorData.AcceptChanges()
            lstrQuery = "[" + mstrMappedInvName + "] ='" + pstrInvestorName + "'"
            larrRepeatedInvestorName = ldtInvestorData.Select(lstrQuery)
            If larrRepeatedInvestorName.Length > 1 Then
                For Each lRow As DataRow In larrRepeatedInvestorName
                    If ldtInvestorData.Rows.IndexOf(lRow) <> pintCurrentRowIndex Then
                        If (lRow.Item("Investor Type").ToString.Equals("") AndAlso lRow.Item("Master Investor Type").ToString.Equals("") AndAlso lRow.Item("Head Investor").ToString.Equals("") _
                            AndAlso lRow.Item("Style").ToString.Equals("") AndAlso lRow.Item("Turnover").ToString.Equals("") AndAlso lRow.Item("City").ToString.Equals("") _
                            AndAlso lRow.Item("State").ToString.Equals("") AndAlso lRow.Item("Country Name").ToString.Equals("")) _
                        OrElse _
                        ( _
                            (lRow.Item("Investor Type").ToString.Equals("") = False AndAlso lRow.Item("Investor Type").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                          OrElse _
                          (lRow.Item("Master Investor Type").ToString.Equals("") = False AndAlso lRow.Item("Master Investor Type").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                          OrElse (lRow.Item("Head Investor").ToString.Equals("") = False AndAlso lRow.Item("Head Investor").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                            OrElse (lRow.Item("Style").ToString.Equals("") = False AndAlso lRow.Item("Style").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                            OrElse (lRow.Item("Turnover").ToString.Equals("") = False AndAlso lRow.Item("Turnover").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                            OrElse (lRow.Item("City").ToString.Equals("") = False AndAlso lRow.Item("City").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                            OrElse (lRow.Item("State").ToString.Equals("") = False AndAlso lRow.Item("State").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                            OrElse (lRow.Item("Country Name").ToString.Equals("") = False AndAlso lRow.Item("Country Name").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                            ) _
                         Then
                            Return True
                        End If
                    End If
                Next
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
            Return False
        Finally
            ldtInvestorData = Nothing
            larrRepeatedInvestorName = Nothing
        End Try
        Return False
    End Function

    Private Sub grdInvestors_BeforeEnterEditMode(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles grdInvestors.BeforeEnterEditMode
        If grdInvestors.ActiveCell Is Nothing Then Exit Sub
        If grdInvestors.ActiveCell.Column.Header.Caption.Equals(mstrMappedInvName) = False Then
            If HasChildRepeatedRowAccessForEnter(grdInvestors.ActiveCell.Row.GetCellText(grdInvestors.Rows.Band.Columns(mstrMappedInvName)), grdInvestors.ActiveRow.ListIndex) Then
                e.Cancel = True
                Exit Sub
            End If
        End If
        mintInvestorClickedRowIndex = grdInvestors.ActiveRow.Index
        AllowInvestorEdit(grdInvestors, marrAllowEditInvestor)
        'If cell is not editable then cancel the edit mode
        If marrAllowEditInvestor.Contains(grdInvestors.ActiveCell.Column.Header.Caption) = False Then e.Cancel = True
    End Sub

    Private Sub grdInvestors_BeforeExitEditMode(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeExitEditModeEventArgs) Handles grdInvestors.BeforeExitEditMode
        Try
            If grdInvestors.ActiveCell Is Nothing Then Exit Sub
            If grdInvestors.ActiveCell.Text.Equals("") Then Exit Sub
            If grdInvestors.ActiveCell.Column.Header.Caption.Equals(mstrMappedInvName) Then
                If HasMainRepeatedRowForExit(grdInvestors.ActiveCell.Text, grdInvestors.ActiveRow.ListIndex) Then
                    SetInvestorRowAsRepeated(grdInvestors.ActiveRow)
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        Finally
        End Try
    End Sub

    Private Sub grdInvestors_ClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.ClickCellEventArgs) Handles grdInvestors.ClickCell
        If grdInvestors.ActiveCell Is Nothing Then Exit Sub
        AllowInvestorEdit(grdInvestors, marrAllowEditInvestor)
    End Sub

    Private Sub grdInvestors_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grdInvestors.KeyDown
        Try
            Dim keyText As Integer
            If e.Modifiers = Keys.Control Then
                'To unmap the selected rows
                grdInvestors.Selected.Rows.Sort()

                If e.KeyCode = Keys.U AndAlso grdInvestors.ActiveRow IsNot Nothing Then
                    Dim lblnZeroIsSelectedRow As Boolean = False
                    If grdInvestors.Selected.Rows.Count = 0 Then
                        lblnZeroIsSelectedRow = True
                        grdInvestors.Rows(grdInvestors.ActiveRow.Index).Selected = True
                    End If
                    For Each lRow As UltraGridRow In grdInvestors.Selected.Rows
                        lRow.Cells(mstrMappedInvName).Value = mstrUnmapped
                    Next
                    grdInvestors.UpdateData()
                    If lblnZeroIsSelectedRow Then
                        grdInvestors.Rows(grdInvestors.ActiveRow.Index).Selected = False
                    End If
                End If
                If grdInvestors.Selected.Rows.Count > 1 Then
                    If e.KeyValue > 48 And e.KeyValue < 58 Then
                        'grdInvestors.EventManager.SetEnabled(Infragistics.Win.UltraWinGrid.GridEventIds.AfterCellUpdate, False)
                        For i As Integer = 0 To grdInvestors.Selected.Rows.Count - 1
                            If i = 0 Then Continue For
                            keyText = e.KeyValue - 49
                            If grdInvestors.Selected.Rows(0).Cells(marrAllowEditInvestor(keyText)).Value IsNot DBNull.Value Then
                                If HasChildRepeatedRowAccessForEnter(grdInvestors.Selected.Rows(i).GetCellText(grdInvestors.Rows.Band.Columns(mstrMappedInvName)), grdInvestors.Selected.Rows(i).ListIndex) = False Then
                                    If grdInvestors.Selected.Rows(0).Cells(marrAllowEditInvestor(keyText)).Value <> String.Empty Then
                                        If marrAllowEditInvestor(keyText) = marrAllowEditInvestor(2) Then
                                            grdInvestors.Selected.Rows(i).Cells(marrAllowEditInvestor(keyText - 1)).Value = grdInvestors.Selected.Rows(0).Cells(marrAllowEditInvestor(keyText - 1)).Value
                                        End If
                                        grdInvestors.Selected.Rows(i).Cells(marrAllowEditInvestor(keyText)).Value = grdInvestors.Selected.Rows(0).Cells(marrAllowEditInvestor(keyText)).Value
                                    Else
                                        Me.ShowMessageBox(DBResult.ES_ROW_BLANK_DATA, marrAllowEditInvestor(keyText))
                                        Exit Sub
                                    End If
                                End If
                            Else
                                Me.ShowMessageBox(DBResult.ES_ROW_BLANK_DATA, marrAllowEditInvestor(keyText))
                                Exit Sub
                            End If
                            If HasMainRepeatedRowForExit(grdInvestors.Selected.Rows(i).GetCellText(grdInvestors.Rows.Band.Columns(mstrMappedInvName)), grdInvestors.Selected.Rows(i).ListIndex) Then
                                SetInvestorRowAsRepeated(grdInvestors.Selected.Rows(i))
                            End If
                        Next
                    End If
                End If

            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
        Finally
            grdInvestors.UpdateData()
            'grdInvestors.EventManager.SetEnabled(Infragistics.Win.UltraWinGrid.GridEventIds.AfterCellUpdate, True)
        End Try
    End Sub

    Private Sub grdFunds_BeforeEnterEditMode(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles grdFunds.BeforeEnterEditMode
        If grdFunds.ActiveCell Is Nothing Then Exit Sub
        If grdFunds.ActiveCell.Column.Header.Caption.Equals(mstrMappedFundName) = False Then
            If HasChildRepeatedFundAccessForEnter(grdFunds.ActiveCell.Row.GetCellText(grdFunds.Rows.Band.Columns(mstrMappedFundName)), grdFunds.ActiveRow.ListIndex) Then
                e.Cancel = True
                Exit Sub
            End If
        End If
        AllowInvestorEdit(grdFunds, marrAllowEditFund)
        'If cell is not editable then cancel the edit mode
        If marrAllowEditFund.Contains(grdFunds.ActiveCell.Column.Header.Caption) = False Then e.Cancel = True
    End Sub

    Private Sub grdFunds_BeforeExitEditMode(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeExitEditModeEventArgs) Handles grdFunds.BeforeExitEditMode
        Try
            If grdFunds.ActiveCell Is Nothing Then Exit Sub
            If grdFunds.ActiveCell.Text.Equals("") Then Exit Sub
            If grdFunds.ActiveCell.Column.Header.Caption.Equals(mstrMappedFundName) Then
                If HasMainRepeatedFundForExit(grdFunds.ActiveCell.Text, grdFunds.ActiveRow.ListIndex) Then
                    SetFundRowAsRepeated(grdFunds.ActiveRow)
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        Finally
        End Try
    End Sub

    Private Function HasChildRepeatedFundAccessForEnter(ByVal pstrFundName As String, ByVal lintCurrentRowIndex As Integer) As Boolean
        Dim lblnStatus As Boolean = False
        Dim ldtFundData As DataTable = Nothing
        Dim larrRepeatedName() As DataRow = Nothing
        Dim lstrQuery As String
        Dim lRow As DataRow = Nothing
        Try
            If pstrFundName.Equals("") Then Return False
            grdFunds.UpdateData()
            ldtFundData = grdFunds.DataSource
            ldtFundData.AcceptChanges()
            lstrQuery = "[" + mstrMappedFundName + "] ='" + pstrFundName + "'"
            larrRepeatedName = ldtFundData.Select(lstrQuery)
            If larrRepeatedName.Length > 1 Then
                lRow = ldtFundData.Rows(lintCurrentRowIndex)
                If (lRow.Item("Fund Type").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) AndAlso _
                    lRow.Item("Style").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) AndAlso _
                    lRow.Item("Turnover").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) AndAlso _
                    lRow.Item("Country Name").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString)) _
                    Then
                    Return True
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
            Return False
        Finally
            ldtFundData = Nothing
            larrRepeatedName = Nothing
        End Try
        Return False
    End Function

    Private Function HasMainRepeatedFundForExit(ByVal pstrFundName As String, ByVal pintCurrentRowIndex As Integer) As Boolean
        Dim lblnStatus As Boolean = False
        Dim ldtFundData As DataTable = Nothing
        Dim larrRepeatedName() As DataRow = Nothing
        Dim lstrQuery As String
        Try
            If pstrFundName.Equals("") Then Return False
            grdFunds.UpdateData()
            ldtFundData = grdFunds.DataSource
            ldtFundData.AcceptChanges()
            lstrQuery = "[" + mstrMappedFundName + "] ='" + pstrFundName + "'"
            larrRepeatedName = ldtFundData.Select(lstrQuery)
            If larrRepeatedName.Length > 1 Then
                For Each lRow As DataRow In larrRepeatedName
                    If ldtFundData.Rows.IndexOf(lRow) <> pintCurrentRowIndex Then
                        If (lRow.Item("Fund Type").ToString.Equals("") AndAlso lRow.Item("Style").ToString.Equals("") AndAlso lRow.Item("Turnover").ToString.Equals("") _
                            AndAlso lRow.Item("Country Name").ToString.Equals("")) _
                        OrElse _
                        ( _
                            (lRow.Item("Fund Type").ToString.Equals("") = False AndAlso lRow.Item("Fund Type").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                            OrElse (lRow.Item("Style").ToString.Equals("") = False AndAlso lRow.Item("Style").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                            OrElse (lRow.Item("Turnover").ToString.Equals("") = False AndAlso lRow.Item("Turnover").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                            OrElse (lRow.Item("Country Name").ToString.Equals("") = False AndAlso lRow.Item("Country Name").ToString.Equals(CommonCLL.m_strSelectIDValue.ToString) = False) _
                            ) _
                         Then
                            Return True
                        End If
                    End If
                Next
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
            Return False
        Finally
            ldtFundData = Nothing
            larrRepeatedName = Nothing
        End Try
        Return False
    End Function

    Private Sub grdFunds_ClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.ClickCellEventArgs) Handles grdFunds.ClickCell
        If grdFunds.ActiveCell Is Nothing Then Exit Sub
        AllowInvestorEdit(grdFunds, marrAllowEditFund)
    End Sub

    Private Sub grdFunds_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grdFunds.KeyDown
        Try
            Dim keyText As Integer
            grdFunds.Selected.Rows.Sort()
            If e.Modifiers = Keys.Control Then
                'To unmap the selected rows
                If e.KeyCode = Keys.U AndAlso grdFunds.ActiveRow IsNot Nothing Then
                    Dim lblnZeroIsSelectedRow As Boolean = False
                    If grdFunds.Selected.Rows.Count = 0 Then
                        lblnZeroIsSelectedRow = True
                        grdFunds.Rows(grdFunds.ActiveRow.Index).Selected = True
                    End If
                    For Each lRow As UltraGridRow In grdFunds.Selected.Rows
                        lRow.Cells(mstrMappedFundName).Value = mstrUnmapped
                    Next
                    grdFunds.UpdateData()
                    If lblnZeroIsSelectedRow Then
                        grdFunds.Rows(grdFunds.ActiveRow.Index).Selected = False
                    End If
                End If
                If grdFunds.Selected.Rows.Count > 1 Then
                    If e.KeyValue > 48 And e.KeyValue < 54 Then
                        'grdFunds.EventManager.SetEnabled(Infragistics.Win.UltraWinGrid.GridEventIds.AfterCellUpdate, False)
                        For i As Integer = 0 To grdFunds.Selected.Rows.Count - 1
                            If i = 0 Then Continue For
                            keyText = e.KeyValue - 49
                            If HasChildRepeatedFundAccessForEnter(grdFunds.Selected.Rows(i).GetCellText(grdFunds.Rows.Band.Columns(mstrMappedFundName)), grdFunds.Selected.Rows(i).ListIndex) = False Then
                                If grdFunds.Selected.Rows(0).Cells(marrAllowEditFund(keyText)).Value IsNot DBNull.Value Then
                                    If grdFunds.Selected.Rows(0).Cells(marrAllowEditFund(keyText)).Value <> String.Empty Then
                                        grdFunds.Selected.Rows(i).Cells(marrAllowEditFund(keyText)).Value = grdFunds.Selected.Rows(0).Cells(marrAllowEditFund(keyText)).Value
                                    Else
                                        Me.ShowMessageBox(DBResult.ES_ROW_BLANK_DATA, marrAllowEditFund(keyText))
                                    End If
                                End If
                            End If
                            If HasMainRepeatedFundForExit(grdFunds.Selected.Rows(i).GetCellText(grdFunds.Rows.Band.Columns(mstrMappedFundName)), grdFunds.Selected.Rows(i).ListIndex) Then
                                SetFundRowAsRepeated(grdFunds.Selected.Rows(i))
                            End If
                        Next
                    End If
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
        Finally
            grdFunds.UpdateData()
            'grdFunds.EventManager.SetEnabled(Infragistics.Win.UltraWinGrid.GridEventIds.AfterCellUpdate, True)
        End Try
    End Sub

    Private Sub grdInvestors_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdInvestors.InitializeLayout
        Try
            If grdInvestors.DataSource Is Nothing Then Exit Sub

            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Investor Type").EditorComponent = Me.cmbInvestorType
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Investor Type").Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDownList

            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Master Investor Type").EditorComponent = Me.cmbMasterInvestorType
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Master Investor Type").Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDownList

            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Head Investor").EditorComponent = Me.cmbHeadInvestor
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Head Investor").Style = ColumnStyle.DropDownList

            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Style").EditorComponent = Me.cmbStyle
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Style").Style = ColumnStyle.DropDownList

            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Turnover").EditorComponent = Me.cmbTurnover
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Turnover").Style = ColumnStyle.DropDownList

            Me.grdInvestors.DisplayLayout.Bands(0).Columns("City").EditorComponent = Me.cmbCity
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("City").Style = ColumnStyle.DropDownList

            Me.grdInvestors.DisplayLayout.Bands(0).Columns("State").EditorComponent = Me.cmbState
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("State").Style = ColumnStyle.DropDownList

            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Country Name").EditorComponent = Me.cmbCountry
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Country Name").Style = ColumnStyle.DropDownList

            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Investor Type").Width = 150
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Master Investor Type").Width = 150
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Head Investor").Width = 250
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Style").Width = 100
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Turnover").Width = 100
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("City").Width = 100
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("State").Width = 100
            Me.grdInvestors.DisplayLayout.Bands(0).Columns("Country Name").Width = 100

        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        End Try
    End Sub

    Private Sub grdFunds_InitializeLayout(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles grdFunds.InitializeLayout
        Try
            If grdFunds.DataSource Is Nothing Then Exit Sub

            Me.grdFunds.DisplayLayout.Bands(0).Columns("Fund Type").EditorComponent = Me.cmbFundType
            Me.grdFunds.DisplayLayout.Bands(0).Columns("Fund Type").Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDownList

            Me.grdFunds.DisplayLayout.Bands(0).Columns("Style").EditorComponent = Me.cmbFundStyle
            Me.grdFunds.DisplayLayout.Bands(0).Columns("Style").Style = ColumnStyle.DropDownList

            Me.grdFunds.DisplayLayout.Bands(0).Columns("Turnover").EditorComponent = Me.cmbFundTurnover
            Me.grdFunds.DisplayLayout.Bands(0).Columns("Turnover").Style = ColumnStyle.DropDownList

            Me.grdFunds.DisplayLayout.Bands(0).Columns("Country Name").EditorComponent = Me.cmbFundCountry
            Me.grdFunds.DisplayLayout.Bands(0).Columns("Country Name").Style = ColumnStyle.DropDownList

            Me.grdFunds.DisplayLayout.Bands(0).Columns("Fund Type").Width = 150
            Me.grdFunds.DisplayLayout.Bands(0).Columns("Style").Width = 100
            Me.grdFunds.DisplayLayout.Bands(0).Columns("Turnover").Width = 100
            Me.grdFunds.DisplayLayout.Bands(0).Columns("Country Name").Width = 100
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        End Try
    End Sub

    Private Sub grdInvestors_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdInvestors.MouseDown
        Dim mousePoint As Point = Nothing
        mousePoint = New Point(e.X, e.Y)
        mintInvestorClickedRowIndex = GetClickedRowIndex(grdInvestors, mousePoint)
    End Sub

    Private Function GetClickedRowIndex(ByVal grdData As CrystalUltraGrid, ByVal mousePoint As Point) As Integer
        Dim el As UIElement = grdData.DisplayLayout.UIElement.ElementFromPoint(mousePoint)
        Dim cell As UltraGridCell = CommonCLL.GetCell(el)
        Dim row As UltraGridRow = CommonCLL.GetRow(el)
        Dim lintRowIndex As Integer = -1
        If row IsNot Nothing Then
            lintRowIndex = row.Index
        ElseIf cell IsNot Nothing Then
            lintRowIndex = cell.Row.Index
        End If
        Return lintRowIndex
    End Function

#End Region

#Region "Button Events"

    Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        'If there is no row to be inserted, then show validation
        If grdFinalizeInvestor.Rows.VisibleRowCount = 0 AndAlso grdFinalizeFund.Rows.VisibleRowCount = 0 Then
            ShowMessageBox(DBResult.EDS_NEW_MARK_CRETE_ROW)
            Exit Sub
        End If
        Dim ldtNewInvestors As New DataTable
        Dim ldtNewFunds As New DataTable
        Dim lintColCount As Int16 = 0
        Dim lintArrColCount As Int16 = 0
        Dim larrExcludingCols(1) As String
        Dim lstrInvestorData As String
        Dim lstrFundData As String
        Dim ldsExistingData As DataSet = Nothing
        Dim larrInvestorKeyCols() As String = {""}
        Dim larrFundKeyCols() As String = {""}
        'Dim larrInvestorKeyCols() As String = {"Investor Type", "Master Investor Type", "Head Investor", "Style", "Turnover", "City", "State", "Country Name"}
        'Dim larrFundKeyCols() As String = {"Fund Type", "Style", "Turnover", "Country Name"}
        Try
            ''Prepare datatable to generate xml
            ldtNewInvestors = grdFinalizeInvestor.DataSource
            'Exclude columns/values that are not going to be used in insert process
            While lintColCount < ldtNewInvestors.Columns.Count
                If marrAllowEditInvestor.Contains(ldtNewInvestors.Columns(lintColCount).ColumnName) = False Then
                    larrExcludingCols(lintArrColCount) = ldtNewInvestors.Columns(lintColCount).ColumnName
                    ReDim Preserve larrExcludingCols(lintArrColCount + 1)
                    lintArrColCount += 1
                End If
                lintColCount += 1
            End While
            ReDim Preserve larrExcludingCols(lintArrColCount - 1)
            lstrInvestorData = CommonCLL.GetXMlFromUltraGrid(grdFinalizeInvestor, larrExcludingCols, larrInvestorKeyCols, True)
            'lstrInvestorData = CommonCLL.GetXMlFromDataTable(ldtNewInvestors, larrExcludingCols)

            lintColCount = 0
            lintArrColCount = 0
            ReDim larrExcludingCols(1)
            'Prepare datatable to generate xml
            ldtNewFunds = grdFinalizeFund.DataSource
            'Exclude columns/values that are not going to be used in insert process
            While lintColCount < ldtNewFunds.Columns.Count
                If marrAllowEditFund.Contains(ldtNewFunds.Columns(lintColCount).ColumnName) = False Then
                    larrExcludingCols(lintArrColCount) = ldtNewFunds.Columns(lintColCount).ColumnName
                    ReDim Preserve larrExcludingCols(lintArrColCount + 1)
                    lintArrColCount += 1
                End If
                lintColCount += 1
            End While
            ReDim Preserve larrExcludingCols(lintArrColCount - 1)
            'Create XML
            lstrFundData = CommonCLL.GetXMlFromUltraGrid(grdFinalizeFund, larrExcludingCols, larrFundKeyCols, True)
            'lstrFundData = CommonCLL.GetXMlFromDataTable(ldtNewFunds, larrExcludingCols)

            ldsExistingData = New DataSet
            Dim lstrTableNames(2) As String
            Dim lblnIsInvestorSucess As Boolean = False
            Dim lblnIsFundSucess As Boolean = False
            'Check if the records exist; If yes then get a list of those which already exists, otherwise Insert them.
            ldsExistingData = mclsCreateInvestorFundManager.CheckAndCreateNew(lstrInvestorData, lstrFundData, mintSourceTypeID, CommonCLL.UserID, lblnIsInvestorSucess, lblnIsFundSucess)
            If ldsExistingData.Tables.Count > 0 Then
                ldsExistingData.Tables(0).TableName = "Investors"
                If ldsExistingData.Tables.Count = 1 Then
                    If ldsExistingData.Tables(0).Rows(0).Item("ErrorMessage") IsNot DBNull.Value Then
                        MessageBox.Show(ldsExistingData.Tables(0).Rows(0).Item("ErrorMessage").ToString, MessageManager.GetMessage("CRYSTAL"), MessageBoxButtons.OK)
                        Exit Sub
                    End If
                ElseIf ldsExistingData.Tables.Count = 3 Then
                    If ldsExistingData.Tables(2).Rows(0).Item("ErrorMessage") IsNot DBNull.Value Then
                        MessageBox.Show(ldsExistingData.Tables(2).Rows(0).Item("ErrorMessage").ToString, MessageManager.GetMessage("CRYSTAL"), MessageBoxButtons.OK)
                        Exit Sub
                    End If
                End If
                ldsExistingData.Tables(1).TableName = "Funds"
                'If Investor & Fund both created successfully
                If lblnIsInvestorSucess AndAlso lblnIsFundSucess Then
                    mdtNewInvestors = grdFinalizeInvestor.DataSource
                    'Add MInvestorTypeID column in the datatable
                    mdtNewInvestors.Columns.Add("MInvestorTypeID", GetType(Int16))
                    mdtNewFunds = grdFinalizeFund.DataSource
                    Dim lintRowCounter As Integer = 0
                    Dim lstrQuery As String
                    Dim larrFoundRows() As DataRow

                    'Set the Investors' attributes that will be copied in Map Data grid
                    For Each lRow As DataRow In ldsExistingData.Tables("Investors").Rows
                        lstrQuery = "[" + mstrMappedInvName + "]='" + lRow.Item("InvestorName") + "'"
                        larrFoundRows = mdtNewInvestors.Select(lstrQuery)
                        For Each lNewRow As DataRow In larrFoundRows
                            lNewRow.Item("InvestorID") = lRow.Item("InvestorID")
                            lNewRow.Item("MInvestorTypeID") = lRow.Item("MInvestorTypeID")
                            lNewRow.Item("InvEntityID") = lRow.Item("InvEntityID")
                        Next
                    Next
                    'Set the Funds'' attributes that will be copied in Map Data grid
                    For Each lRow As DataRow In ldsExistingData.Tables("Funds").Rows
                        lstrQuery = "[" + mstrMappedFundName + "]='" + lRow.Item("FundName") + "'"
                        larrFoundRows = mdtNewFunds.Select(lstrQuery)
                        For Each lNewRow As DataRow In larrFoundRows
                            lNewRow.Item("FundID") = lRow.Item("FundID")
                            lNewRow.Item("FundEntityID") = lRow.Item("FundEntityID")
                        Next
                    Next
                    ShowMessageBox(DBResult.EDS_CRETE_SUCCESS_RECRD)
                    Me.Close()
                Else
                    ShowMessageBox(DBResult.EDS_CRETE_EXSTNG_RECRD)
                    Dim lfrmExistingInvestorFund As New ExistingInvestorFund(CType(grdInvestors.DataSource, DataTable), CType(grdFunds.DataSource, DataTable), ldsExistingData.Tables("Investors"), ldsExistingData.Tables("Funds"), lblnIsInvestorSucess, lblnIsFundSucess)
                    lfrmExistingInvestorFund.Show()
                End If
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        Finally
            ldtNewInvestors = Nothing
            ldtNewFunds = Nothing
            larrExcludingCols = Nothing
            larrInvestorKeyCols = Nothing
            larrFundKeyCols = Nothing
            ldsExistingData = Nothing
        End Try
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        tbCreateNew.PerformAction(Infragistics.Win.UltraWinTabControl.UltraTabControlAction.SelectNextTab)
    End Sub

#End Region

#Region "Tabpage Events"
    Private Sub tbCreateNew_ActiveTabChanging(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinTabControl.ActiveTabChangingEventArgs) Handles tbCreateNew.ActiveTabChanging
        Dim lblnIsAllowed As Boolean = True
        ValidateNextAction(lblnIsAllowed)
        'If validations failed, then don't move to the next tabpage.
        If lblnIsAllowed = False Then
            e.Cancel = True
        End If
    End Sub

    Private Sub tbCreateNew_SelectedTabChanged(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinTabControl.SelectedTabChangedEventArgs) Handles tbCreateNew.SelectedTabChanged
        Try
            If e.Tab.Index = 2 Then
                btnNext.Visible = False
                btnCreate.Visible = True
                btnCreate.Location = btnNext.Location
            Else
                btnNext.Visible = True
                btnCreate.Visible = False
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, mintProjectID)
        Finally
        End Try
    End Sub
#End Region


#Region "Combo box Events"

    Private Sub cmbCity_BeforeDropDown(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cmbCity.BeforeDropDown
        If mintInvestorClickedRowIndex = -1 Then Exit Sub

        If grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Country Name") IsNot DBNull.Value Then
            Dim lintSelectedCountryID As Int16 = grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Country Name")
            For Each lRow As UltraGridRow In cmbCity.Rows
                If lRow.GetCellValue("CountryID") Is DBNull.Value Then
                    lRow.Hidden = True
                    Continue For
                End If
                If CType(lRow.GetCellValue("CountryID"), Integer).Equals(lintSelectedCountryID) Then
                    lRow.Hidden = False
                Else
                    lRow.Hidden = True
                End If
            Next
        End If
        If CType(sender, Infragistics.Win.UltraWinGrid.UltraCombo).Name = "cmbCity" Then
            If grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Country Name") Is DBNull.Value Then
                For Each lRow As UltraGridRow In cmbCity.Rows
                    lRow.Hidden = True
                Next
            End If
        End If
    End Sub

    Private Sub cmbState_BeforeDropDown(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cmbState.BeforeDropDown
        If mintInvestorClickedRowIndex = -1 Then Exit Sub

        If grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Country Name") IsNot DBNull.Value Then
            Dim lintSelectedCountryID As Int16 = grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Country Name")
            For Each lRow As UltraGridRow In cmbState.Rows
                If lRow.GetCellValue("CountryID") Is DBNull.Value Then
                    lRow.Hidden = True
                    Continue For
                End If
                If CType(lRow.GetCellValue("CountryID"), Integer).Equals(lintSelectedCountryID) Then
                    lRow.Hidden = False
                Else
                    lRow.Hidden = True
                End If
            Next
        End If
        If CType(sender, Infragistics.Win.UltraWinGrid.UltraCombo).Name = "cmbState" Then
            If grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Country Name") Is DBNull.Value Then
                For Each lRow As UltraGridRow In cmbState.Rows
                    lRow.Hidden = True
                Next
            End If
        End If
    End Sub

    Private Sub cmbMasterInvestorType_BeforeDropDown(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cmbMasterInvestorType.BeforeDropDown
        If mintInvestorClickedRowIndex = -1 Then Exit Sub

        If grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Investor Type") IsNot DBNull.Value Then
            Dim lintSelectedInvestorTypeID As Int16 = grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Investor Type")
            For Each lRow As UltraGridRow In cmbMasterInvestorType.Rows
                If lRow.GetCellValue("InvestorTypeID") Is DBNull.Value Then Continue For
                If CType(lRow.GetCellValue("InvestorTypeID"), Integer).Equals(lintSelectedInvestorTypeID) Then
                    lRow.Hidden = False
                Else
                    lRow.Hidden = True
                End If
            Next
        End If
        If CType(sender, Infragistics.Win.UltraWinGrid.UltraCombo).Name = "cmbMasterInvestorType" Then
            If grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Investor Type") Is DBNull.Value Then
                For Each lRow As UltraGridRow In cmbMasterInvestorType.Rows
                    lRow.Hidden = True
                Next
            End If
        End If
    End Sub
    ''' <summary>
    ''' To hide/show the InvestorType and MasterInvestoType rows in comboboxes based on the selection of one another
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cmbInvestorType_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbInvestorType.ValueChanged, cmbMasterInvestorType.ValueChanged, cmbHeadInvestor.ValueChanged, cmbStyle.ValueChanged, cmbTurnover.ValueChanged, cmbCity.ValueChanged, cmbState.ValueChanged, cmbCountry.ValueChanged
        Try
            grdInvestors.UpdateData()
            If grdInvestors.ActiveRow Is Nothing Then Exit Sub
            If mintInvestorClickedRowIndex = -1 Then Exit Sub
            Dim ldrDetailRow() As DataRow = Nothing
            If CType(sender, Infragistics.Win.UltraWinGrid.UltraCombo).Name = "cmbInvestorType" AndAlso grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Investor Type") IsNot DBNull.Value AndAlso CType(grdInvestors.ActiveRow.Cells("Investor Type").Column.EditorComponent, UltraCombo).Value IsNot Nothing Then
                'Dim lintSelectedInvestorTypeID As Int16 = CType(grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("Investor Type").Column.EditorComponent, UltraCombo).Value
                'For Each lRow As UltraGridRow In cmbMasterInvestorType.Rows
                '    If lRow.GetCellValue("InvestorTypeID") Is DBNull.Value Then Continue For
                '    If CType(lRow.GetCellValue("InvestorTypeID"), Integer).Equals(lintSelectedInvestorTypeID) Then
                '        lRow.Hidden = False
                '    Else
                '        lRow.Hidden = True
                '    End If
                'Next
                If grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("Master Investor Type").Value IsNot DBNull.Value Then
                    ldrDetailRow = CType(cmbMasterInvestorType.DataSource, DataTable).Select("MInvestorTypeID=" + _
                                                                    grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("Master Investor Type").Value + " AND InvestorTypeID=" + _
                                                                    grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("Investor Type").Value)
                    If ldrDetailRow Is Nothing Then
                        grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("Master Investor Type").Value = -1
                    ElseIf ldrDetailRow.Length = 0 Then
                        grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("Master Investor Type").Value = -1
                    End If
                End If
            ElseIf CType(sender, Infragistics.Win.UltraWinGrid.UltraCombo).Name = "cmbCountry" Then
                ldrDetailRow = CType(cmbCity.DataSource, DataTable).Select("CityID=" + _
                                                                    grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("City").Value + " AND CountryID=" + _
                                                                    grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("Country Name").Value)
                If ldrDetailRow Is Nothing Then
                    grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("City").Value = -1
                ElseIf ldrDetailRow.Length = 0 Then
                    grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("City").Value = -1
                End If

                If grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("State").Value IsNot DBNull.Value Then
                    ldrDetailRow = CType(cmbState.DataSource, DataTable).Select("StateID=" + _
                                                                                        grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("State").Value + " AND CountryID=" + _
                                                                                        grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("Country Name").Value)
                    If ldrDetailRow Is Nothing Then
                        grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("State").Value = -1
                    ElseIf ldrDetailRow.Length = 0 Then
                        grdInvestors.Rows(mintInvestorClickedRowIndex).Cells("State").Value = -1
                    End If
                End If
            End If
            'If CType(sender, Infragistics.Win.UltraWinGrid.UltraCombo).Name = "cmbMasterInvestorType" Then
            '    If grdInvestors.Rows(mintInvestorClickedRowIndex).GetCellValue("Investor Type") Is DBNull.Value Then
            '        For Each lRow As UltraGridRow In cmbMasterInvestorType.Rows
            '            lRow.Hidden = True
            '        Next
            '    End If
            'End If
            grdInvestors.ActiveRow.Update()
            'If CType(sender, Infragistics.Win.UltraWinGrid.UltraCombo).Name = "cmbInvestorType" AndAlso grdInvestors.ActiveRow.GetCellValue(grdInvestors.ActiveRow.Cells("Investor Type").Column) IsNot DBNull.Value AndAlso CType(grdInvestors.ActiveRow.Cells("Investor Type").Column.EditorComponent, UltraCombo).Value IsNot Nothing Then
            '    Dim lintSelectedInvestorTypeID As Int16 = CType(grdInvestors.ActiveRow.Cells("Investor Type").Column.EditorComponent, UltraCombo).Value
            '    For Each lRow As UltraGridRow In cmbMasterInvestorType.Rows
            '        If lRow.GetCellValue(lRow.Cells("InvestorTypeID").Column) Is DBNull.Value Then Continue For
            '        If CType(lRow.GetCellValue(lRow.Cells("InvestorTypeID").Column), Integer).Equals(lintSelectedInvestorTypeID) Then
            '            lRow.Hidden = False
            '        Else
            '            lRow.Hidden = True
            '        End If
            '    Next
            'End If
            'If CType(sender, Infragistics.Win.UltraWinGrid.UltraCombo).Name = "cmbMasterInvestorType" Then
            '    If grdInvestors.ActiveRow.GetCellValue(grdInvestors.ActiveRow.Cells("Investor Type").Column) Is DBNull.Value Then
            '        For Each lRow As UltraGridRow In cmbMasterInvestorType.Rows
            '            lRow.Hidden = True
            '        Next
            '    End If
            'End If
            'grdInvestors.ActiveRow.Update()
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        End Try
    End Sub


    Private Sub cmbFundType_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbFundType.ValueChanged, cmbFundStyle.ValueChanged, cmbFundTurnover.ValueChanged, cmbFundCountry.ValueChanged
        If grdFunds.ActiveRow Is Nothing Then Exit Sub
        grdFunds.ActiveRow.Update()
    End Sub

#End Region

#End Region

#Region "Procedures"
    ''' <summary>
    ''' Load all comboboxes
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitLoadAllCombos()
        Dim dr As DataRow
        Dim ldtMasterData As DataTable = Nothing
        Dim ldsMasterData As DataSet = Nothing
        Dim IDR As IDataReader = Nothing
        Dim lstrErrorHandler(8) As String
        Try
            IDR = mclsCreateInvestorFundManager.GetMasterAttributes()
            ldsMasterData = New DataSet
            ldsMasterData.Load(IDR, LoadOption.OverwriteChanges, lstrErrorHandler)
            CommonCLL.ChangeDataReaderDataSet(ldsMasterData)
            ldsMasterData.Tables(0).TableName = "InvestorType"
            ldsMasterData.Tables(1).TableName = "MasterInvestorType"
            ldsMasterData.Tables(2).TableName = "HeadInvestor"
            ldsMasterData.Tables(3).TableName = "Style"
            ldsMasterData.Tables(4).TableName = "Turnover"
            ldsMasterData.Tables(5).TableName = "City"
            ldsMasterData.Tables(6).TableName = "State"
            ldsMasterData.Tables(7).TableName = "Country"
            ldsMasterData.Tables(8).TableName = "FundType"

            '--------------------Investor Type Starts-----------------------
            cmbInvestorType.DisplayMember = "Investor Type"
            cmbInvestorType.ValueMember = "InvestorTypeID"
            ldtMasterData = ldsMasterData.Tables("InvestorType")
            If AddRowInDropDownAtFirst(ldtMasterData, cmbInvestorType.ValueMember, cmbInvestorType.DisplayMember, CommonCLL.m_strSelectIDValue, CommonCLL.m_strSelectTextValue) = False Then
                ShowMessageBox("Error in accessing Sub Investor Type drop down.")
                Exit Sub
            End If
            cmbInvestorType.DataSource = ldtMasterData
            cmbInvestorType.DisplayLayout.Bands(0).Columns("InvestorTypeID").Hidden = True
            'cmbInvestorType.DisplayLayout.Bands(0).Columns("MInvestorTypeID").Hidden = True
            cmbInvestorType.DisplayLayout.Bands(0).Columns("Investor Type").PerformAutoResize(cmbInvestorType.Rows.Count)
            '--------------------Investor Type Ends-----------------------

            '--------------------Master Investor Type Starts-----------------------
            cmbMasterInvestorType.DisplayMember = "Master Investor Type"
            cmbMasterInvestorType.ValueMember = "MInvestorTypeID"
            'ldsMasterData.Tables("MasterInvestorType").Columns("MInvestorType").ColumnName = "Master Investor Type"
            ldtMasterData = ldsMasterData.Tables("MasterInvestorType")
            If AddRowInDropDownAtFirst(ldtMasterData, cmbMasterInvestorType.ValueMember, cmbMasterInvestorType.DisplayMember, CommonCLL.m_strSelectIDValue, CommonCLL.m_strSelectTextValue) = False Then
                ShowMessageBox("Error in accessing Master Investor Type drop down.")
                Exit Sub
            End If
            cmbMasterInvestorType.DataSource = ldtMasterData
            cmbMasterInvestorType.DisplayLayout.Bands(0).Columns("MInvestorTypeID").Hidden = True
            cmbMasterInvestorType.DisplayLayout.Bands(0).Columns("InvestorTypeID").Hidden = True
            Dim larrInstRow() As DataRow
            larrInstRow = CType(cmbMasterInvestorType.DataSource, DataTable).Select("MInvestorTypeID=1")
            If larrInstRow.Length > 0 Then
                cmbMasterInvestorType.SelectedRow = cmbMasterInvestorType.Rows(CType(cmbMasterInvestorType.DataSource, DataTable).Rows.IndexOf(larrInstRow(0)))
            End If
            cmbMasterInvestorType.DisplayLayout.Bands(0).Columns("Master Investor Type").PerformAutoResize(cmbMasterInvestorType.Rows.Count)
            '--------------------Master Investor Type Ends-----------------------

            '--------------------Head Investor Starts-----------------------
            cmbHeadInvestor.DisplayMember = "Head Investor"
            cmbHeadInvestor.ValueMember = "HeadInvestorID"
            ldtMasterData = ldsMasterData.Tables("HeadInvestor")
            If AddRowInDropDownAtFirst(ldtMasterData, cmbHeadInvestor.ValueMember, cmbHeadInvestor.DisplayMember, CommonCLL.m_strSelectIDValue, CommonCLL.m_strSelectTextValue) = False Then
                ShowMessageBox("Error in accessing Master Investor Type drop down.")
                Exit Sub
            End If
            cmbHeadInvestor.DataSource = ldtMasterData
            'cmbSuperholder.DisplayLayout.Bands(0).Columns("Superholder").Hidden = True
            'larrInstRow = CType(cmbSuperholder.DataSource, DataTable).Select("MInvestorTypeID=1")
            'If larrInstRow.Length > 0 Then
            '    cmbSuperholder.SelectedRow = cmbSuperholder.Rows(CType(cmbSuperholder.DataSource, DataTable).Rows.IndexOf(larrInstRow(0)))
            'End If
            cmbHeadInvestor.DisplayLayout.Bands(0).Columns("HeadInvestorID").Hidden = True
            cmbHeadInvestor.DisplayLayout.Bands(0).Columns("Head Investor").PerformAutoResize(cmbHeadInvestor.Rows.Count)
            '--------------------Master Investor Type Ends-----------------------

            '--------------------Style Starts-----------------------
            cmbStyle.DisplayMember = "Style"
            cmbStyle.ValueMember = "Styleid"
            ldtMasterData = ldsMasterData.Tables("Style")
            If AddRowInDropDownAtFirst(ldtMasterData, cmbStyle.ValueMember, cmbStyle.DisplayMember, CommonCLL.m_strSelectIDValue, CommonCLL.m_strSelectTextValue) = False Then
                ShowMessageBox("Error in accessing Style drop down.")
                Exit Sub
            End If
            cmbStyle.DataSource = ldtMasterData
            cmbStyle.DisplayLayout.Bands(0).Columns("Styleid").Hidden = True
            cmbStyle.DisplayLayout.Bands(0).Columns("Style").PerformAutoResize(cmbStyle.Rows.Count)

            cmbFundStyle.DisplayMember = "Style"
            cmbFundStyle.ValueMember = "Styleid"
            cmbFundStyle.DataSource = ldtMasterData.Copy
            cmbFundStyle.DisplayLayout.Bands(0).Columns("Styleid").Hidden = True
            cmbFundStyle.DisplayLayout.Bands(0).Columns("Style").PerformAutoResize(cmbStyle.Rows.Count)

            'cmbStyle.SelectedRow = cmbStyle.Rows(0)
            '--------------------Style Ends-----------------------

            '--------------------Turnover Starts-----------------------
            cmbTurnover.DisplayMember = "Turnover"
            cmbTurnover.ValueMember = "TurnoverID"
            ldtMasterData = ldsMasterData.Tables("Turnover")
            If AddRowInDropDownAtFirst(ldtMasterData, cmbTurnover.ValueMember, cmbTurnover.DisplayMember, CommonCLL.m_strSelectIDValue, CommonCLL.m_strSelectTextValue) = False Then
                ShowMessageBox("Error in accessing Turnover drop down.")
                Exit Sub
            End If
            cmbTurnover.DataSource = ldtMasterData
            cmbTurnover.DisplayLayout.Bands(0).Columns("TurnoverID").Hidden = True
            cmbTurnover.DisplayLayout.Bands(0).Columns("Turnover").PerformAutoResize(cmbTurnover.Rows.Count)

            cmbFundTurnover.DisplayMember = "Turnover"
            cmbFundTurnover.ValueMember = "TurnoverID"
            cmbFundTurnover.DataSource = ldtMasterData.Copy
            cmbFundTurnover.DisplayLayout.Bands(0).Columns("TurnoverID").Hidden = True
            cmbFundTurnover.DisplayLayout.Bands(0).Columns("Turnover").PerformAutoResize(cmbTurnover.Rows.Count)
            '--------------------Turnover Ends-----------------------

            '--------------------City Starts-----------------------
            cmbCity.DisplayMember = "City"
            cmbCity.ValueMember = "CityID"
            ldtMasterData = ldsMasterData.Tables("City")
            If AddRowInDropDownAtFirst(ldtMasterData, cmbCity.ValueMember, cmbCity.DisplayMember, CommonCLL.m_strSelectIDValue, CommonCLL.m_strSelectTextValue) = False Then
                ShowMessageBox("Error in accessing City drop down.")
                Exit Sub
            End If
            cmbCity.DataSource = ldtMasterData
            cmbCity.DisplayLayout.Bands(0).Columns("CityID").Hidden = True
            cmbCity.DisplayLayout.Bands(0).Columns("CountryID").Hidden = True
            cmbCity.DisplayLayout.Bands(0).Columns("City").PerformAutoResize(cmbCity.Rows.Count)
            'cmbCity.SelectedRow = cmbCity.Rows(0)
            '--------------------City Ends-----------------------

            '--------------------State Starts-----------------------
            cmbState.DisplayMember = "State"
            cmbState.ValueMember = "StateID"
            ldtMasterData = ldsMasterData.Tables("State")
            '     dr = DataTable.NewRow
            If AddRowInDropDownAtFirst(ldtMasterData, cmbState.ValueMember, cmbState.DisplayMember, CommonCLL.m_strSelectIDValue, CommonCLL.m_strSelectTextValue) = False Then
                ShowMessageBox("Error in accessing State drop down.")
                Exit Sub
            End If
            cmbState.DataSource = ldtMasterData
            cmbState.DisplayLayout.Bands(0).Columns("StateID").Hidden = True
            cmbState.DisplayLayout.Bands(0).Columns("CountryID").Hidden = True
            cmbState.DisplayLayout.Bands(0).Columns("State").PerformAutoResize(cmbState.Rows.Count)
            'cmbState.SelectedRow = cmbState.Rows(0)
            '--------------------State Ends-----------------------

            '--------------------Country Starts-----------------------
            cmbCountry.DisplayMember = "Country Name"
            cmbCountry.ValueMember = "CountryID"
            ldtMasterData = ldsMasterData.Tables("Country")
            mdtCounty = ldtMasterData
            If AddRowInDropDownAtFirst(ldtMasterData, cmbCountry.ValueMember, cmbCountry.DisplayMember, CommonCLL.m_strSelectIDValue, CommonCLL.m_strSelectTextValue) = False Then
                ShowMessageBox("Error in accessing Country drop down.")
                Exit Sub
            End If

            cmbCountry.DataSource = ldtMasterData
            cmbCountry.DisplayLayout.Bands(0).Columns("CountryID").Hidden = True
            cmbCountry.DisplayLayout.Bands(0).Columns("country").Hidden = True
            cmbCountry.DisplayLayout.Bands(0).Columns("countrycode").Hidden = True
            cmbCountry.DisplayLayout.Bands(0).Columns("Country Name").PerformAutoResize(cmbCountry.Rows.Count)

            cmbFundCountry.DisplayMember = "Country Name"
            cmbFundCountry.ValueMember = "CountryID"
            cmbFundCountry.DataSource = ldtMasterData.Copy
            cmbFundCountry.DisplayLayout.Bands(0).Columns("CountryID").Hidden = True
            cmbFundCountry.DisplayLayout.Bands(0).Columns("country").Hidden = True
            cmbFundCountry.DisplayLayout.Bands(0).Columns("countrycode").Hidden = True
            cmbFundCountry.DisplayLayout.Bands(0).Columns("Country Name").PerformAutoResize(cmbCountry.Rows.Count)
            '--------------------Country Ends-----------------------

            '--------------------Fund Type Starts-----------------------
            cmbFundType.DisplayMember = "Fund Type"
            cmbFundType.ValueMember = "FundTypeID"
            ldtMasterData = ldsMasterData.Tables("FundType")
            If AddRowInDropDownAtFirst(ldtMasterData, cmbFundType.ValueMember, cmbFundType.DisplayMember, CommonCLL.m_strSelectIDValue, CommonCLL.m_strSelectTextValue) = False Then
                ShowMessageBox("Error in accessing Fund Type drop down.")
                Exit Sub
            End If
            cmbFundType.DataSource = ldtMasterData
            cmbFundType.DisplayLayout.Bands(0).Columns("FundTypeID").Hidden = True
            '--------------------Fund Type Ends-----------------------
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        Finally
            dr = Nothing
            ldtMasterData = Nothing
            ldsMasterData = Nothing
            IDR = Nothing
            lstrErrorHandler = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' Add a value in drop down at first place
    ''' </summary>
    ''' <param name="dataTable">Data Table to access</param>
    ''' <param name="idColumn">ID Column of row</param>
    ''' <param name="valueColumn">Text Column of row</param>
    ''' <param name="rowValue">ID Value of row</param>
    ''' <param name="rowText">Text Value of row</param>
    ''' <remarks></remarks>
    Public Function AddRowInDropDownAtFirst(ByRef dataTable As DataTable, ByVal idColumn As String, ByVal valueColumn As String, ByVal rowValue As Integer, ByVal rowText As String) As Boolean
        Try
            If CommonCLL.CheckEmptyDataTable(dataTable) Then
                Return False
            End If
            Dim drDataRow As DataRow
            drDataRow = dataTable.NewRow
            drDataRow.Item(idColumn) = rowValue
            drDataRow.Item(valueColumn) = rowText
            dataTable.Rows.InsertAt(drDataRow, 0)
            Return True
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
            Return False
        End Try
    End Function

    ''' <summary>
    ''' To set the CellActivation
    ''' </summary>
    ''' <param name="grdData"></param>
    ''' <param name="marrAllowEdit"></param>
    ''' <remarks></remarks>
    Private Sub AllowInvestorEdit(ByVal grdData As CrystalUltraGrid, ByVal marrAllowEdit() As String)
        ' If the active column is allowed for editing, then Allow Edit otherwise, CellSelect
        If marrAllowEdit.Contains(grdData.ActiveCell.Column.Header.Caption) Then
            grdData.ActiveCell.Column.CellActivation = Infragistics.Win.UltraWinGrid.Activation.AllowEdit
            grdData.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.Edit
        Else
            grdData.ActiveCell.Column.CellActivation = Infragistics.Win.UltraWinGrid.Activation.ActivateOnly
            grdData.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect
        End If
    End Sub

    ''' <summary>
    ''' Validate the tab page
    ''' </summary>
    ''' <param name="pblnIsAllowed"></param>
    ''' <remarks></remarks>
    Private Sub ValidateNextAction(Optional ByRef pblnIsAllowed As Boolean = True)
        If tbCreateNew.ActiveTab Is Nothing Then Exit Sub
        Dim lstrQuery As String
        Dim larrChangedRowNumber() As DataRow
        Dim lstCheckedInvestors As List(Of String)
        Dim lstCheckedFunds As List(Of String)
        Dim ldtInvestorData As DataTable = Nothing
        Dim ldtFundData As DataTable = Nothing
        Dim ldrMainRow As DataRow = Nothing
        Dim lintMainRowIndex As Integer = -1
        Dim lintTraversingDuplicateIndex As Integer
        Try
            lstCheckedInvestors = New List(Of String)
            lstCheckedFunds = New List(Of String)
            If tbCreateNew.ActiveTab.Index = 0 Then
                grdFinalizeInvestor.DataSource = CType(grdInvestors.DataSource, DataTable).Copy
                ldtInvestorData = grdFinalizeInvestor.DataSource
                For Each lRow As DataRow In ldtInvestorData.Rows
                    If lRow.Item(mstrMappedInvName).ToString.Equals("") Or lRow.Item(mstrMappedInvName).ToString.Equals(mstrUnmapped) Then Continue For
                    If lstCheckedInvestors.Count > 0 Then
                        If lstCheckedInvestors.Contains(lRow.Item(mstrMappedInvName).ToString) Then Continue For
                    End If
                    lstrQuery = "[" + mstrMappedInvName + "] ='" + lRow.Item(mstrMappedInvName).ToString + "'"
                    larrChangedRowNumber = ldtInvestorData.Select(lstrQuery)
                    'If same Investor Name exists multiple times, then attributes set the attributes of all the repeating rows too.
                    If larrChangedRowNumber.Length > 1 Then
                        For Each lDupRow As DataRow In larrChangedRowNumber
                            If lDupRow.Item("Investor Type") Is DBNull.Value Then
                                Exit For
                            End If
                            If lDupRow.Item("Investor Type") <> "-1" Then
                                ldrMainRow = lDupRow
                                lintMainRowIndex = ldtInvestorData.Rows.IndexOf(ldrMainRow)
                                Exit For
                            End If
                        Next
                        If lintMainRowIndex >= 0 Then
                            For Each lDupRow As DataRow In larrChangedRowNumber
                                lintTraversingDuplicateIndex = ldtInvestorData.Rows.IndexOf(lDupRow)
                                If lintTraversingDuplicateIndex = lintMainRowIndex Then
                                    If lstCheckedInvestors.Contains(lDupRow.Item(mstrMappedInvName).ToString) = False Then
                                        lstCheckedInvestors.Add(lDupRow.Item(mstrMappedInvName).ToString)
                                    End If
                                Else
                                    lDupRow.Item("Investor Type") = ldtInvestorData.Rows(lintMainRowIndex).Item("Investor Type")
                                    lDupRow.Item("Master Investor Type") = ldtInvestorData.Rows(lintMainRowIndex).Item("Master Investor Type")
                                    lDupRow.Item("Head Investor") = ldtInvestorData.Rows(lintMainRowIndex).Item("Head Investor")
                                    lDupRow.Item("Style") = ldtInvestorData.Rows(lintMainRowIndex).Item("Style")
                                    lDupRow.Item("Turnover") = ldtInvestorData.Rows(lintMainRowIndex).Item("Turnover")
                                    lDupRow.Item("City") = ldtInvestorData.Rows(lintMainRowIndex).Item("City")
                                    lDupRow.Item("State") = ldtInvestorData.Rows(lintMainRowIndex).Item("State")
                                    lDupRow.Item("Country Name") = ldtInvestorData.Rows(lintMainRowIndex).Item("Country Name")

                                    lDupRow.Item("Investor Type Name") = ldtInvestorData.Rows(lintMainRowIndex).Item("Investor Type Name")
                                    lDupRow.Item("Master Investor Type Name") = ldtInvestorData.Rows(lintMainRowIndex).Item("Master Investor Type Name")
                                    lDupRow.Item("Head Investor Name") = ldtInvestorData.Rows(lintMainRowIndex).Item("Head Investor Name")
                                    lDupRow.Item("Style Name") = ldtInvestorData.Rows(lintMainRowIndex).Item("Style Name")
                                    lDupRow.Item("Turnover Name") = ldtInvestorData.Rows(lintMainRowIndex).Item("Turnover Name")
                                    lDupRow.Item("City Name") = ldtInvestorData.Rows(lintMainRowIndex).Item("City Name")
                                    lDupRow.Item("State Name") = ldtInvestorData.Rows(lintMainRowIndex).Item("State Name")
                                    lDupRow.Item("CountryName") = ldtInvestorData.Rows(lintMainRowIndex).Item("CountryName")

                                    grdFinalizeInvestor.Rows(lintTraversingDuplicateIndex).Hidden = True
                                End If
                            Next
                        End If
                    End If
                Next
                'Check if the State is not valid for US
                For Each lRow As UltraGridRow In grdFinalizeInvestor.Rows
                    If lRow.GetCellValue(lRow.Cells(mstrMappedInvName).Column).Equals(mstrUnmapped) Then Continue For
                    For Each lCell As UltraGridCell In lRow.Cells
                        If marrAllowEditInvestor.Contains(lCell.Column.Header.Caption) AndAlso lCell.Column.Header.Caption <> mstrMappedInvName AndAlso lCell.Column.Header.Caption <> "Head Investor" Then
                            If lCell.Column.Header.Caption = "State" Then
                                cmbCountry.Text = "United States(US)"
                                If lCell.Row.GetCellValue(lCell.Row.Cells("Country Name").Column).Equals(cmbCountry.SelectedRow.Cells("CountryID").Value.ToString) = False Then
                                    Continue For
                                End If
                            End If
                            If lCell.Row.GetCellValue(lCell.Column).Equals("") OrElse lCell.Row.GetCellValue(lCell.Column).Equals(DBNull.Value) OrElse lCell.Row.GetCellValue(lCell.Column).Equals(CommonCLL.m_strSelectIDValue.ToString) OrElse lCell.Row.GetCellValue(lCell.Column).Equals(CommonCLL.m_strSelectTextValue) Then
                                ShowMessageBox(DBResult.EDS_CRETE_VALIDT_BLANK, "'" + BaseClass.Capitalize(lCell.Column.Header.Caption) + "'")
                                'lCell.Activated = True
                                grdInvestors.Rows(lCell.Row.Index).Cells(lCell.Column.Header.Caption).Activated = True
                                grdInvestors.Focus()
                                pblnIsAllowed = False
                                Exit Sub
                            End If
                        End If
                    Next
                Next
                For Each lRow As UltraGridRow In grdFinalizeInvestor.Rows
                    If lRow.GetCellValue(lRow.Cells(mstrMappedInvName).Column).Equals(mstrUnmapped) Then lRow.Hidden = True
                Next
                MoveNextScreen(grdInvestors, mstrMappedInvName, pblnIsAllowed)
            ElseIf tbCreateNew.ActiveTab.Index = 1 Then
                grdFinalizeFund.DataSource = CType(grdFunds.DataSource, DataTable).Copy
                ldtFundData = grdFinalizeFund.DataSource
                For Each lRow As DataRow In CType(grdFunds.DataSource, DataTable).Rows
                    If lRow.Item(mstrMappedFundName).ToString.Equals("") Or lRow.Item(mstrMappedFundName).ToString.Equals(mstrUnmapped) Then Continue For
                    If lstCheckedFunds.Count > 0 Then
                        If lstCheckedFunds.Contains(lRow.Item(mstrMappedFundName).ToString) Then Continue For
                    End If
                    lstrQuery = "[" + mstrMappedFundName + "] ='" + lRow.Item(mstrMappedFundName).ToString + "'"
                    larrChangedRowNumber = ldtFundData.Select(lstrQuery)
                    'If same Investor Name exists multiple times, then attributes must match too.
                    If larrChangedRowNumber.Length > 1 Then
                        For Each lDupRow As DataRow In larrChangedRowNumber
                            If lDupRow.Item("Fund Type").ToString <> "-1" Then
                                ldrMainRow = lDupRow
                                lintMainRowIndex = ldtFundData.Rows.IndexOf(ldrMainRow)
                                Exit For
                            End If
                        Next
                        If lintMainRowIndex >= 0 Then
                            For Each lDupRow As DataRow In larrChangedRowNumber
                                lintTraversingDuplicateIndex = ldtFundData.Rows.IndexOf(lDupRow)
                                If lintTraversingDuplicateIndex = lintMainRowIndex Then
                                    If lstCheckedFunds.Contains(lDupRow.Item(mstrMappedFundName).ToString) = False Then
                                        lstCheckedFunds.Add(lDupRow.Item(mstrMappedFundName).ToString)
                                    End If
                                Else
                                    lDupRow.Item("Fund Type") = ldtFundData.Rows(lintMainRowIndex).Item("Fund Type")
                                    lDupRow.Item("Style") = ldtFundData.Rows(lintMainRowIndex).Item("Style")
                                    lDupRow.Item("Turnover") = ldtFundData.Rows(lintMainRowIndex).Item("Turnover")
                                    lDupRow.Item("Country Name") = ldtFundData.Rows(lintMainRowIndex).Item("Country Name")

                                    lDupRow.Item("Fund Type Name") = ldtFundData.Rows(lintMainRowIndex).Item("Fund Type Name")
                                    lDupRow.Item("Style Name") = ldtFundData.Rows(lintMainRowIndex).Item("Style Name")
                                    lDupRow.Item("Turnover Name") = ldtFundData.Rows(lintMainRowIndex).Item("Turnover Name")
                                    lDupRow.Item("CountryName") = ldtFundData.Rows(lintMainRowIndex).Item("CountryName")
                                    grdFinalizeFund.Rows(lintTraversingDuplicateIndex).Hidden = True
                                End If
                            Next
                        End If
                    End If
                Next
                'Check if some attribute is left blank, if yes, inform the user and exit
                For Each lRow As UltraGridRow In grdFinalizeFund.Rows
                    If lRow.GetCellValue(lRow.Cells(mstrMappedFundName).Column).Equals(mstrUnmapped) Then Continue For
                    For Each lCell As UltraGridCell In lRow.Cells
                        If marrAllowEditFund.Contains(lCell.Column.Header.Caption) AndAlso lCell.Column.Header.Caption <> mstrMappedFundName Then
                            If lCell.Row.GetCellValue(lCell.Column).Equals("") OrElse lCell.Row.GetCellValue(lCell.Column).Equals(DBNull.Value) OrElse lCell.Row.GetCellValue(lCell.Column).Equals(CommonCLL.m_strSelectIDValue.ToString) OrElse lCell.Row.GetCellValue(lCell.Column).Equals(CommonCLL.m_strSelectTextValue) Then
                                ShowMessageBox(DBResult.EDS_CRETE_VALIDT_BLANK, BaseClass.Capitalize(lCell.Column.Header.Caption))
                                grdFunds.Rows(lCell.Row.Index).Cells(lCell.Column.Header.Caption).Activated = True
                                grdFunds.Focus()
                                pblnIsAllowed = False
                                Exit Sub
                            End If
                        End If
                    Next
                Next
                For Each lRow As UltraGridRow In grdFinalizeFund.Rows
                    If lRow.GetCellValue(lRow.Cells(mstrMappedFundName).Column).Equals(mstrUnmapped) Then lRow.Hidden = True
                Next
                MoveNextScreen(grdFunds, mstrMappedFundName, pblnIsAllowed)
            End If
        Catch ex As Exception
            Me.FormulateAndLogError(ex, mstrScreenName, 0)
        Finally
            larrChangedRowNumber = Nothing
            ldtInvestorData = Nothing
            ldtFundData = Nothing
        End Try
    End Sub

    Private Sub MoveNextScreen(ByVal grdData As UltraGrid, ByVal pstrDataItemName As String, ByRef pblnIsAllowed As Boolean)
        For Each lRow As UltraGridRow In grdData.Rows
            If lRow.Hidden Then Continue For
            If lRow.GetCellValue(lRow.Cells(pstrDataItemName).Column).ToString.Trim.Equals("") Then
                Me.ShowMessageBox(DBResult.EDS_MAPALL_NEXT)
                pblnIsAllowed = False
                Exit Sub
            End If
        Next
    End Sub

#End Region

End Class
